package com.stephen.redisdemo.reference;

import java.lang.ref.WeakReference;

/**
 * @author: stephen
 * @create: 2020-03-03 14:36
 * @description: 弱引用
 * 只要发生gc,都会被回收
 */
public class WeakReferenceDemo {

    public static void main(String[] args) {
        Object o1 = new Object();
        WeakReference<Object> weakReference = new WeakReference<>(o1);
        System.out.println(o1);
        System.out.println(weakReference.get());
        o1 = null;
        System.gc();
        System.out.println("gc之后");
        System.out.println(o1); // null
        System.out.println(weakReference.get());// null
    }
}
